package task4;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class DoublingSequence {
	
	public ArrayList<BigInteger> resultSequence = new ArrayList<BigInteger>();

	class DoublingThread implements Runnable {
		private static final int DEFAULT_STEPS_NUMBER = 10;

		private int stepsNumber;

		DoublingThread() {
			this(DEFAULT_STEPS_NUMBER);
		}

		DoublingThread(int n) {
			stepsNumber = n;
		}

		public void run() {
			for (int i = 0; i < stepsNumber; i++) {				
				resultSequence.add(next());
			}
		}
	}

	private AtomicReference<BigInteger> currentNumber = new AtomicReference<BigInteger>();
	
	DoublingSequence() {
		currentNumber.getAndSet(new BigInteger("1"));		
	}

	public void reset() {
		currentNumber.getAndSet(new BigInteger("1"));
	}

	public BigInteger get() {
		return currentNumber.get();
    }

	public BigInteger next() {
		BigInteger bi, gi;
		do {
            bi = currentNumber.get();
        }
        while (!currentNumber.compareAndSet(bi, gi = bi.multiply(new BigInteger("2"))));
		return gi;
	}
}